home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / spincursor.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  3.9 KB  |  138 lines

  1. /*
  2.     File:        SpinCursor.cp
  3.  
  4.     Contains:    TSpinCursor is a simple cursor spinning class.
  5.                   SpinCursor.cp contains the TSpinCursor member functions.
  6.  
  7.  
  8.     Written by: Kent Sandvik    
  9.  
  10.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #ifndef _SPINCURSOR_
  26. #include "SpinCursor.h"
  27. #endif
  28.  
  29.  
  30. //    CONSTRUCTORS AND DESTRUCTORS
  31. #pragma segment SpinCursor
  32. TSpinCursor::TSpinCursor(short acurID,
  33.                          short spinDirection,
  34.                          short spinTicks)
  35. // Default constructor, bind 'acur' and class together by specifying used 'acur' resource.
  36. {
  37.     fCursorHandle = NULL;
  38.     fSpinDirection = spinDirection;
  39.     fTickCounter = 0;
  40.     fSpinTicks = spinTicks;                        // how many ticks between spins
  41.  
  42.     fCursorList = (AnimationCursRec * *)::GetResource('acur', acurID);
  43.     fError = ::ResError();
  44.     VASSERT(fError == noErr, ("Problems with GetResource = %d", fError));
  45.  
  46.     if (fCursorList != NULL)
  47.     {
  48.         ::HNoPurge((Handle)fCursorList);        // don't purge this resource during use!
  49.  
  50.         for (fIndex = 0; fIndex < (**fCursorList).information.count; fIndex++)
  51.         {
  52.             fCursorID = HiWord(((**fCursorList).nCursors[fIndex]));
  53.             fCursorHandle = ::GetCursor(fCursorID);
  54.             ASSERT(fCursorHandle != NULL, "\pGetCursor returned NULL handle");
  55.  
  56.             (**fCursorList).nCursors[fIndex] = fCursorHandle;
  57.  
  58.             if (fCursorHandle != NULL)
  59.                 ::HNoPurge((Handle)fCursorHandle);// make every CURS handle non-purgeable
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65. #pragma segment SpinCursor
  66. TSpinCursor::~TSpinCursor()
  67. // Clean up after the spinning, handles and such…
  68. {
  69.     CursHandle tempHandle;
  70.     short index;
  71.  
  72.     if (fCursorList != NULL)                    // sanity check
  73.     {
  74.         for (index = 0; index < (**fCursorList).information.count; index++)
  75.         {
  76.             tempHandle = (**fCursorList).nCursors[index];
  77.             if (tempHandle != NULL)
  78.                 ::HPurge((Handle)tempHandle);
  79.         }
  80.         ::ReleaseResource((Handle)fCursorList);    // don't dispose resources
  81.         fError = ResError();
  82.         VASSERT(fError == noErr, ("Problems with ReleaseResource = %d", fError));
  83.     }
  84. }
  85.  
  86.  
  87. // MAIN INTERFACE
  88.  
  89. #pragma segment SpinCursor
  90. void TSpinCursor::Spin()
  91. // Spin the cursor once (next frame) into earlier defined direction.
  92. {
  93.     long ticks;                                    // temp value
  94.     short count;                                // # of animated cursors
  95.     short frameNum;                                // # of frame
  96.  
  97.     if (fCursorList != NULL)                    // sanity check
  98.     {
  99.         ticks = ::TickCount();                    // get the stamp
  100.  
  101.         if ((ticks - fTickCounter) > fSpinTicks)// enough ticks between spins?
  102.         // …then spin, change new cursor, increase the frame count (next CURS),
  103.         // and save the new tick stamp.
  104.         {
  105.             count = (**fCursorList).information.count;
  106.             frameNum = (**fCursorList).frame % count;
  107.             fCursorHandle = (**fCursorList).nCursors[frameNum];
  108.  
  109.             if (fCursorHandle != NULL)            // sanity check
  110.             {
  111.                 ::HLock((Handle)fCursorHandle);    // lock the resource
  112.                 ::SetCursor(&(**fCursorHandle));// set the new CURS
  113.                 ::HUnlock((Handle)fCursorHandle);// unlock it
  114.             }
  115.             (**fCursorList).frame = (frameNum + count + fSpinDirection) % count;
  116.         }
  117.     }
  118. }
  119.  
  120. #pragma segment SpinCursor
  121. void TSpinCursor::SetSpinDirection(EDirection direction)
  122. // Set direction of spin, -1 backwards, 1 = forwards…
  123. {
  124.     fSpinDirection = direction;
  125. }
  126.  
  127.  
  128. // _________________________________________________________________________________________________________ //
  129.  
  130.  
  131. /*    Change History (most recent last):
  132.   No        Init.    Date        Comment
  133.   1            khs        12/14/92    New file
  134.   2            khs        1/3/93        Cleanup
  135. */
  136.  
  137.  
  138.